﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace SharePointSocialNetworking.Webparts.FacebookLikeButtonWebPart
{
    class FacebookLikeButtonEditorPart : EditorPart
    {
        #region private members
        private Label _labelLayout, _labelFaces, _labelWidth, _labelVerb, _labelFont, _labelScheme;
        private TextBox _tbWidth;
        private DropDownList _ddlLayout, _ddlVerb, _ddlFont, _ddlScheme;
        private CheckBox _cbFaces;
        private Dictionary<string, string> _dicLayoutHeights;
        private Dictionary<string, List<string>> _dicDropdownValues;
        #endregion

        #region Constractor
        public FacebookLikeButtonEditorPart()
        {
            this.ID = "EditTool";
            this.Title = "Facebook Like Button Web Part Settings";
            FillDictionraies();
        }
        #endregion

        #region Private methods
        private void FillDictionraies()
        {
            _dicDropdownValues = new Dictionary<string, List<string>>();
            _dicDropdownValues.Add("Layout", new List<string>() { "standard", "button_count", "box_count" });
            _dicDropdownValues.Add("Verb", new List<string>() { "like", "recommend" });
            _dicDropdownValues.Add("Font", new List<string>() { "arial", "lucida+grande", "segoe+ui", "tahoma", "trebuchet+ms", "verdana" });
            _dicDropdownValues.Add("Color", new List<string>() { "light", "dark" });

            _dicLayoutHeights = new Dictionary<string, string>();
            _dicLayoutHeights.Add("standard", "35");
            _dicLayoutHeights.Add("button_count", "21");
            _dicLayoutHeights.Add("box_count", "65");
        }
        #endregion

        #region Overrides

        protected override void CreateChildControls()
        {
            _labelLayout = new Label() { Text = "Layout: " };
            this.Controls.Add(_labelLayout);

            _ddlLayout = new DropDownList();
            _ddlLayout.DataSource = _dicDropdownValues["Layout"];
            _ddlLayout.DataBind();
            this.Controls.Add(_ddlLayout);

            Image qImg = new Image();
            qImg.ImageUrl = SPContext.Current.Web.Url+ "/_catalogs/SocialSharePoint/Images/question-button.png";
            qImg.CssClass = "questionImg";
            qImg.ToolTip = "Determines the size and amount of social context next to the button";
            this.Controls.Add(qImg);
            this.Controls.Add(new Literal() { Text = "<br/><br/>" });

            _labelFaces = new Label() { Text = "Faces: " };
            this.Controls.Add(_labelFaces);

            _cbFaces = new CheckBox();
            _cbFaces.Text = "Show faces";
            this.Controls.Add(_cbFaces);

            Image qImg2 = new Image();
            qImg2.ImageUrl = SPContext.Current.Web.Url + "/_catalogs/SocialSharePoint/Images/question-button.png";
            qImg2.CssClass = "questionImg";
            qImg2.ToolTip = "Show profile pictures below the button";
            this.Controls.Add(qImg2);
            this.Controls.Add(new Literal() { Text = "<br/><br/>" });


            _labelWidth = new Label() { Text = "Width: " };
            this.Controls.Add(_labelWidth);
            
            _tbWidth = new TextBox();
            this.Controls.Add(_tbWidth);

            Image qImg3 = new Image();
            qImg3.ImageUrl = SPContext.Current.Web.Url + "/_catalogs/SocialSharePoint/Images/question-button.png";
            qImg3.CssClass = "questionImg";
            qImg3.ToolTip = "The width of the plugin in pixels.";
            this.Controls.Add(qImg3);
            this.Controls.Add(new Literal() { Text = "<br/><br/>" });

            _labelVerb = new Label() { Text = "Verb to display: " };
            this.Controls.Add(_labelVerb);

            _ddlVerb = new DropDownList();
            _ddlVerb.DataSource = _dicDropdownValues["Verb"];
            _ddlVerb.DataBind();
            this.Controls.Add(_ddlVerb);

            Image qImg4 = new Image();
            qImg4.ImageUrl = SPContext.Current.Web.Url + "/_catalogs/SocialSharePoint/Images/question-button.png";
            qImg4.CssClass = "questionImg";
            qImg4.ToolTip = "The verb to display in the button. Currently only 'like' and 'recommend' are supported.";
            this.Controls.Add(qImg4);
            this.Controls.Add(new Literal() { Text = "<br/><br/>" });

            _labelFont = new Label() { Text = "Font: " };
            this.Controls.Add(_labelFont);

            _ddlFont = new DropDownList();
            _ddlFont.DataSource = _dicDropdownValues["Font"];
            _ddlFont.DataBind();
            this.Controls.Add(_ddlFont);

            Image qImg5 = new Image();
            qImg5.ImageUrl = SPContext.Current.Web.Url + "/_catalogs/SocialSharePoint/Images/question-button.png";
            qImg5.CssClass = "questionImg";
            qImg5.ToolTip = "The font of the plugin";
            this.Controls.Add(qImg5);
            this.Controls.Add(new Literal() { Text = "<br/><br/>" });

            _labelScheme = new Label() { Text = "Color Scheme: " };
            this.Controls.Add(_labelScheme);

            _ddlScheme = new DropDownList();
            _ddlScheme.DataSource = _dicDropdownValues["Color"];
            _ddlScheme.DataBind();
            this.Controls.Add(_ddlScheme);

            Image qImg6 = new Image();
            qImg6.ImageUrl = SPContext.Current.Web.Url + "/_catalogs/SocialSharePoint/Images/question-button.png";
            qImg6.CssClass = "questionImg";
            qImg6.ToolTip = "The color scheme of the plugin.";
            this.Controls.Add(qImg6);
            this.Controls.Add(new Literal() { Text = "<br/><br/>" });
        }
        
        public override bool ApplyChanges()
        {
            EnsureChildControls();
            FacebookLikeButtonWebPart part = (FacebookLikeButtonWebPart)WebPartToEdit;
            part.Layout = _ddlLayout.SelectedValue;
            part.Faces = _cbFaces.Checked;
            part.FacebookWidth = _tbWidth.Text;
            part.Verb = _ddlVerb.SelectedValue;
            part.FacebookFont = _ddlFont.SelectedValue;
            part.Scheme = _ddlScheme.SelectedValue;
            part.FacebookHeight = _dicLayoutHeights[_ddlLayout.SelectedValue];
            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            FacebookLikeButtonWebPart part = (FacebookLikeButtonWebPart)WebPartToEdit;
            _ddlLayout.SelectedValue = part.Layout;
            _cbFaces.Checked = part.Faces;
            _tbWidth.Text = part.FacebookWidth;
            _ddlVerb.SelectedValue = part.Verb;
            _ddlFont.SelectedValue = part.FacebookFont;
            _ddlScheme.SelectedValue = part.Scheme;
        }

        #endregion
    }
}
